package view;

import connectivity.QueryManager;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import main.WinkelApplication;
import model.Klant;
import model.OrderRule;
import model.Product;

/**
 *
 * @author Jack Sierkstra
 */

public class PaymentSearchCustomer extends JFrame
{    
    private JTable customerTable;
    private DefaultTableModel model;
    
    private String klantnummer;
    private JTextField klantnaam;
    private JTextField adres;
    private JTextField postcode;
    private JTextField woonplaats;
    
   
    
    public PaymentSearchCustomer()
    {
        super();
        
        this.model = new DefaultTableModel();
        
        
        getContentPane().setLayout(new BorderLayout());
        
        JLabel titel = new JLabel("Klant selecteren", SwingConstants.CENTER);
        titel.setFont(WinkelApplication.computeFontSize(2));
        getContentPane().add(titel, BorderLayout.PAGE_START);
        
        getContentPane().add(createScrollPaneAndTable(), BorderLayout.CENTER);
        getContentPane().add(createEditBar(), BorderLayout.PAGE_END);
    }
    
    private JPanel createEditBar() {
        JPanel editBar = new JPanel();
        
        
        JButton saveBtn = new JButton("Opslaan");
        saveBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                
               
                setVisible(false);
                dispose(); 
                
            }
        });
        
        JButton cancelBtn = new JButton("Annuleren");
        cancelBtn.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent ae)
            {
                setVisible(false);
                dispose();
            }
        });
        
        Box btnBar = Box.createHorizontalBox();
        btnBar.add(saveBtn);
        btnBar.add(cancelBtn);
        

        
        editBar.add(btnBar);
        
        return editBar;
    }

    private JScrollPane createScrollPaneAndTable() {
        model.addColumn("Klantnummer");
        model.addColumn("Naam");
        model.addColumn("Postcode");
        model.addColumn("Woonplaats");
        
        // Get all orders
        List<Klant> klanten = WinkelApplication.getQueryManager().getCustomers();
        
        for(Klant o : klanten) {
            model.addRow(new Object[] {
                o.getKlant_id(), 
                o.getNaam(), o.getAdres(), 
                o.getPostcode(), o.getWoonplaats() 
            });
        }
        
        // Create the table
        customerTable = new JTable(model) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        

        
       customerTable.addMouseListener(new MouseAdapter() {
            @Override
          public void mouseClicked(MouseEvent e) {
           
              JTable target = (JTable)e.getSource();
              int row = customerTable.getSelectedRow();
              //int col = table.getSelectedColumn();
              
              
              Object lastobj = customerTable.getModel().getValueAt(row, 0);
              //Integer last = new Integer(lastobj.toString());
              // do some action if appropriate column
              klantnummer = lastobj.toString();
            
          }
        });
        
        return new JScrollPane(customerTable);
    }


}
